﻿using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using ShopForWeb.Service;
using ShopForWeb.UI.Filters;
using ShopForWeb.UI.Models;

namespace ShopForWeb.UI.Controllers;

public class HomeController : Controller
{
  private readonly ILogger<HomeController> _logger;
  private CommodityService _service;

  public HomeController(ILogger<HomeController> logger, CommodityService service)
  {
    _logger = logger;
    _service = service;
  }
  [UserInfoFiler]
  [ServiceFilter(typeof(RedisCacheFilter))]
  public async Task<IActionResult> IndexAsync()
  {
    // List<CommodityViewModel> pvms = await _service.GetByPageAsync(0, 12, "");//""代表没有
    // ViewBag.Data = new
    // {
    //   commoditys = pvms
    // };

    // 以上代码可以写成这个样子，更直观
    ViewBag.Data = new
    {
      commoditys = await _service.GetByPageAsync(0, 12, "")
    };

    return View();
  }

  [UserInfoFiler]
  public IActionResult Privacy()
  {
    return View();
  }

  [UserInfoFiler]
  [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  public IActionResult Error()
  {
    return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  }

  [UserInfoFiler]
  public IActionResult Test1([FromQuery] string name, string password)
  {
    ViewBag.Name = name;
    ViewBag.Password = password;
    return View();
  }
}
